﻿using System;
using System.Text;
using System.Xml.Linq;
using DataGeneration.Domain.Helpers;

namespace DataGeneration.Domain.CustomProcessors
{
    public  class EntityKeyGenerator
    {
        public static void AppendEntitykeyPropertyValue(StringBuilder propertyValues, string entityKeyFilename, XElement property, string patternName, XElement patternEntities)
        {
            bool isForeignEntitykey = property.ContainsAttributeWithoutValue(Tokens.References, "");
            if (isForeignEntitykey)
            {
                int entityKey;
                if (property.ContainsAttributeWithoutValue(Tokens.ActualValue, ""))
                {
                    entityKey = Convert.ToInt32(property.ReadAttributeValue(Tokens.ActualValue));
                }
                else
                {
                    string referenceEntity = property.ReadAttributeValue(Tokens.References);
                    XElement entityNode = patternEntities.RetrieveEntity(referenceEntity);
                    entityKey = Convert.ToInt32(FileHelper.ReadLastEntry(ParameterizedAppSettings.GenerateEntityKeyFilename(patternName, entityNode.ReadAttributeValue(Tokens.TableName))));
                }

                propertyValues.AppendFormat("{0},", entityKey);
                FileHelper.SaveEntityKey(entityKeyFilename, entityKey.ToString());
            }
            else
            {
                if (property.ReadAttributeValue("type").ToLower().Trim().Equals("string"))
                {
                    ///TODO: Replace with proper unique string

                    int entityKey;
                    if (property.ContainsAttributeWithoutValue(Tokens.ActualValue, ""))
                    {
                        entityKey = Convert.ToInt32(property.ReadAttributeValue(Tokens.ActualValue));
                    }
                    else
                    {
                        entityKey = FileHelper.GetNextKey(entityKeyFilename);
                        while (FileHelper.EntityKeyExist(entityKeyFilename, entityKey.ToString()))
                        {
                            entityKey++;
                        }
                    }

                    propertyValues.AppendFormat("'{0}',", entityKey);
                    FileHelper.SaveEntityKey(entityKeyFilename, entityKey.ToString());
                }
                else
                {
                    int entityKey;
                    if (property.ContainsAttributeWithoutValue(Tokens.ActualValue, ""))
                    {
                        entityKey = Convert.ToInt32(property.ReadAttributeValue(Tokens.ActualValue));
                    }
                    else
                    {
                        entityKey = FileHelper.GetNextKey(entityKeyFilename);
                    }

                    while (FileHelper.EntityKeyExist(entityKeyFilename, entityKey.ToString()))
                    {
                        entityKey++;
                    }

                    propertyValues.AppendFormat("{0},", entityKey);
                    FileHelper.SaveEntityKey(entityKeyFilename, entityKey.ToString());
                }
            }
        }
    }
}